﻿Imports System.Data.SqlClient

Partial Class registration
    Inherits System.Web.UI.Page

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn").ToString())
        Dim gender As Char
        If (rbtnMale.Checked) Then
            gender = "m"
        Else
            gender = "f"
        End If
        Try
            Dim sqlcmd As String = "insert into users values('" + txtUserName.Text.ToLower() + "','" + txtPassword.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + DateTime.Now.ToString() + "','" + ddlLocation.SelectedItem.Text + "','" + txtBirthDate.Text + "','" + gender + "','" + txtEmail.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','allowed');"

            cn.Open()
            Dim cmd As SqlCommand = New SqlCommand(sqlcmd, cn)
            Dim i As Integer = cmd.ExecuteNonQuery()
            If i <> 0 Then
                Label2.Text = "You have successfully Registered with our Network."
                Label3.Visible = True
                LinkButton6.Visible = True
                LinkButton7.Visible = True
                btnSubmit.Enabled = False
            End If
            cn.Close()
        Catch ex As Exception
            Label2.Text = "Sorry for inconvience !!! Try again after some time " + ex.Message
            Label2.Visible = True
            cn.Close()
        End Try





    End Sub

    Protected Sub LinkButton7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton7.Click
        Response.Redirect("upload page.aspx")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Label6.Text = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern.ToString

        Label3.Visible = False
        LinkButton6.Visible = False
        LinkButton7.Visible = False

    End Sub

    Protected Sub LinkButton6_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles LinkButton6.Click
        Response.Redirect("search.aspx")
    End Sub


    Protected Sub txtUserName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtUserName.TextChanged
        Label1.Text = ""
        Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn").ToString())
  
        Try
            Dim sqlcmd As String = "select count(*) from users where username='" + txtUserName.Text + "'"
            cn.Open()
            Dim cmd As SqlCommand = New SqlCommand(sqlcmd, cn)

            If cmd.ExecuteScalar = 1 Then
                ' so it means user exists
                Label1.Text = "Username Not available"
                txtUserName.Text = ""
                txtUserName.Focus()
            Else
                Label1.Text = "Congratulation Username is available"
            End If
            cn.Close()
        Catch ex As Exception
            Label2.Text = "Sorry for inconvience !!! Try again after some time " + ex.Message
            Label2.Visible = True
            cn.Close()
        End Try

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Label1.Text = ""
        Dim cn As New SqlConnection(ConfigurationSettings.AppSettings("conn").ToString())

        Try
            Dim sqlcmd As String = "select count(*) from users where username='" + txtUserName.Text + "'"
            cn.Open()
            Dim cmd As SqlCommand = New SqlCommand(sqlcmd, cn)

            If cmd.ExecuteScalar = 1 Then
                ' so it means user exists
                Label1.Text = "Username Not available"
                txtUserName.Text = ""
                txtUserName.Focus()
            Else
                Label1.Text = "Congratulation Username is available"
            End If
            cn.Close()
        Catch ex As Exception
            Label2.Text = "Sorry for inconvience !!! Try again after some time " + ex.Message
            Label2.Visible = True
            cn.Close()
        End Try

    End Sub
End Class
